package com.boang.thread;

import java.util.concurrent.*;

/*
    @author:sa
    @date:2024/11/27 11:26
*/
public class CallableDemo implements Callable<String> {
    @Override
    public String call() throws Exception {
        Thread.sleep(300);
        return "执行完成";
    }
}
class Test{
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        FutureTask futureTask = new FutureTask(new CallableDemo());
        ExecutorService executor=Executors.newSingleThreadExecutor();
        executor.submit(futureTask);
//        futureTask.run();
//        Object obj=futureTask.get();
        try { Object result=futureTask.get(5, TimeUnit.SECONDS);
            System.out.println("任务完成，结果是:"+result);
        }catch (TimeoutException e){
            System.err.println("任务超时");
        }finally {
            executor.shutdown();
        }

    }
}